<template>
  <yd-ls-box>
    <yd-ls-layout title="医生注册" :onfoot=false >
      <div class="yd-ls-height"></div>
      <div>
        <yd-cell-group>
          <yd-cell-item>
            <yd-icon slot="icon" name="phone3"></yd-icon>
            <yd-input slot="right" type="number" v-model="valid.mobileno" regex="mobile" required ref="mobileno" placeholder="请输入手机号码"></yd-input>
          </yd-cell-item>
          <yd-cell-item>
            <yd-icon slot="icon" name="shield-outline"></yd-icon>
            <input type="text"  maxlength="6" slot="right" v-model="valid.code" placeholder="请输入验证码">
            <yd-sendcode slot="right" v-model="incode" @click.native="getScanReg" type="warning"></yd-sendcode>
          </yd-cell-item>
          <yd-cell-item>
            <yd-icon slot="icon" name="verifycode"></yd-icon>
            <yd-input slot="right" type="password" ref="passwords" v-model="valid.password" placeholder="请输入密码"></yd-input>
          </yd-cell-item>
          <yd-cell-item>
            <yd-icon slot="icon" name="share1"></yd-icon>
            <yd-input slot="right" type="text" v-model="valid.invite_code" :disabled="true"></yd-input>
          </yd-cell-item>
        </yd-cell-group>
      </div>
      <div class="yd-ls-reg-tips">
        点击下一步按钮，即表示同意<router-link :to="toPrototcol">< {{comName}}服务协议 ></router-link>
      </div>
    </yd-ls-layout>
    <yd-ls-button btnname="注册" @handle-button="fetchReg"></yd-ls-button>
  </yd-ls-box>
</template>

<style>
  
.yd-ls-reg-tips {
  width: 100%;
  text-align: center;
  color: #919191;
  position: fixed;
  bottom: 80px;
}
.yd-ls-reg-tips a {
  display: inline-block;
  color: #4870f3;
  font-size: 0.28rem;
  font-weight: 600;
  padding: 0 8px;
}
</style>
<script>
  import ydLsBox from "../../components/yd-ls-box.vue"
  import ydLsLayout from '../../components/yd-ls-layout.vue'
  import ydLsButton from "../../components/yd-ls-button.vue"
  import api from '../../axios/api'

  export default {
    name: 'scanReg',
    data() {
      return {
        incode: false,
        valid: {
          mobileno: '',
          password: '',
          code: '',
          invite_code: '',
          drid: ''
        },
        validCode: {},
        toPrototcol: '',
        comName: api.default_title
      }
    },
    created() {
      //TODO:初始化数据
      this.valid.drid = this.$route.query.drid
      this.valid.invite_code = this.$route.query.invite_code
      this.toPrototcol = `/prototcol?drid=${this.valid.drid}&invite_code=${this.valid.invite_code}`
    },
    methods: {
      getScanReg () {
        let phoneValid = this.phoneValid(); // 校验手机号码
        if (!phoneValid) return;

        const gotoDownload = ()=> { this.$router.push({ path: "download" }) }
        const getTips = ()=> {
          this.$dialog.confirm({
            title: '温馨提示',
            mes: `该手机号码已经注册，是否直接前往下载${this.comName}？`,
            opts: [
              { txt: '暂时不了', color: false },
              { txt: '好，去下载', color: true,
                callback: () => {
                  //gotoDownload()
                  window.location.href = api.app_download;
                }
              }
            ]
          });
        }

        this.$dialog.loading.open('发送中...')
        this.axios.get(api.app_scan_validcode, {
            params: {
              mobileno: this.valid.mobileno
            }
          }).then(m => {
            let valid_data = m.data
            this.validCode = valid_data.data
            this.$dialog.loading.close();
            if(this.validCode.is_register) {
              getTips()
            }else {
              this.incode = true;
              this.notify("已发送，请查收！");
            }
          }).catch(err => {
            this.$dialog.loading.close();
            this.notify("发送失败，请重发！");
          });
      
      },
      fetchReg () {
        let phoneValid = this.phoneValid(); // 校验手机号码
        if (!phoneValid) return;

        if (!!!this.valid.code || this.valid.code.length < 6) {
          this.notify("请输入正确的验证码！");
          return;
        }
        this.$dialog.loading.open(api.default_loading_txt);
        this.axios.post(api.app_scan_reg, this.valid).then(m => {
          this.$dialog.loading.close()
          if (m.data.retcode != 0) {
            this.notify(m.data.retmsg);
            return;
          }
          //this.$router.push({ path: "download" });
          window.location.href = api.app_download;
        }).catch(err=>{
            this.$dialog.loading.close()
            this.$dialog.alert({mes:'scan_drRegister=' + JSON.stringify(err)})
          })
      },
      phoneValid() {
        const regPhone = this.$refs.mobileno;
        if (!regPhone.valid) {
          this.notify("请先填写正确的11位数手机号码！");
          return false;
        } else {
          return true;
        }
      },
      notify(info) {
        this.$dialog.notify({
          mes: info,
          timeout: 1500
        });
      }
    },
    mounted() {
    },
    components: { ydLsBox, ydLsLayout, ydLsButton }


  }
</script>
